#! /bin/bash

PGXS=$(dirname `pg_config --pgxs`)
REGRESS="${PGXS}/../test/regress/pg_regress"
TESTDIR="test"
DBNAME=contrib_regression
PRETESTS="setup create-tables load-data index-data"
if [ "x$TEST" = "x" ] ; then
  TESTS=$(ls ${TESTDIR}/sql/test-*.sql ${TESTDIR}/sql/issue-*.sql ${TESTDIR}/sql/syntax-*.sql | \
    cut -f3 -d/ | \
    cut -f1 -d . | \
    sort | \
    grep -v test-llapi | \
    grep -v test-operators | \
    grep -v test-tablesamplers)
  TESTS="${TESTS} legacy/issue-83"
elif [ "$TEST" = "all-syntax-tests" ]; then
  TESTS=$(ls ${TESTDIR}/sql/syntax-*.sql | \
    cut -f3 -d/ | \
    cut -f1 -d . | \
    sort)
elif [ "$TEST" = "all-legacy-tests" ]; then
  TESTS=$(ls ${TESTDIR}/sql/legacy/*.sql | \
    cut -f3- -d/ | \
    cut -f1 -d . | \
    sort -n -k2 -t -)
else
  TESTS=${TEST}
fi

cat ${TESTDIR}/sql/load-data.tmpl | sed -e "s:@PWD:`pwd`:g" > ${TESTDIR}/sql/load-data.sql
cat ${TESTDIR}/sql/load-data.tmpl | sed -e "s:@PWD:`pwd`:g" > ${TESTDIR}/expected/load-data.out


mkdir -p results/legacy > /dev/null
psql -l | grep ${DBNAME} > /dev/null
if [ $? == 1 ]; then
  # load data if we don't have the regression database
  ${REGRESS} --dbname=${DBNAME} --inputdir=${TESTDIR} $PRETESTS || exit $?
fi
#${REGRESS} --use-existing --dbname=${DBNAME} --inputdir=${TESTDIR} --launcher="psql --set=VERBOSITY=terse contrib_regression" $TESTS
${REGRESS} --use-existing --dbname=${DBNAME} --inputdir=${TESTDIR} --launcher="./testlauncher.sh" $TESTS


